const util = require('../../../../../../../../utils/util.js');
const api = require('../../../../../../../../config/api.js');
const user = require('../../../../../../../../services/user.js');

//获取应用实例
const app = getApp()
const openid = app.globalData.logininfo.openid;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    intoDialogShow: false,// 转入本组的提示弹窗
    group: {},// 当前分组
    groupList: [],// 所有分组
    circleid: '',// 圈子的id
    groupJoinUserList: [],// 圈子的用户
    intoIndex: '',// 转入本组的临时下标
    searchText: '',// 搜索的值
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    var group = JSON.parse(options.group);
    var groupList = JSON.parse(options.groupList);
    var circleid = options.circleid;
    _this.setData({
      group: group,
      groupList: groupList,
      circleid: circleid
    })
    
    // 获取分组后的用户信息
    _this.getGroupJoinUser();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 获取分组后的用户信息
   */
  getGroupJoinUser: function(){
    var _this = this;
    if (openid != null && openid != "") {
      // 已登录
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var data = {};
        data.openid = openid;
        data.access_token = res.access_token;
        data.circleid = _this.data.circleid;
        data.searchText = _this.data.searchText;
        util.post(api.Home_GetgroupJoinUserApiUrl, data).then(function (res) {
          if (res.code == 200) {
            _this.setData({
              groupJoinUserList: res.result.list
            })
          }
        })
      })
    }
  },

  /**
   * 用户选中
   */
  groupJoinUserCheck: function (e) {
    var _this = this;
    var index = e.currentTarget.dataset.index;
    var ischeck = _this.data.groupJoinUserList[index].ischeck;
    if (ischeck){
      ischeck = false;
    }else{
      ischeck = true
    }
    _this.setData({
      ["groupJoinUserList[" + index + "].ischeck"]: ischeck
    })
  },

  /**
   * 转入本组弹窗打开
   */
  intoDialogShow: function (e) {
    var _this = this;
    var index = e.currentTarget.dataset.index;
    _this.setData({
      intoIndex: index,
      intoDialogShow: true
    })
  },

  /**
   * 转入本组弹窗关闭
   */
  intoDialogClose: function(e){
    var _this = this;
    _this.setData({
      intoIndex: '',
      intoDialogShow: false
    })
  },

  /**
   * 转入本组：确认
   */
  intoDialogConfirm: function (e) {
    var _this = this;
    var index = _this.data.intoIndex;
    _this.setData({
      ["groupJoinUserList[" + index + "].ischeck"]: true,
      intoDialogShow: false,
      intoIndex: ''
    })
  },

  /**
   * 搜索
   */
  search: function(e){
    var _this = this;
    var value = e.detail.value;
    _this.setData({
      searchText: value
    })
    _this.getGroupJoinUser();
  },

  /**
   * 提交
   */
  submit: function(){
    var _this = this;
    if (openid != null && openid != "") {
      // 已登录
      wx.showLoading({
        title: "提交中"
      })
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var joinUser = [];
        var circleid = _this.data.circleid;
        var groupid = _this.data.group.id;
        var groupJoinUserList = _this.data.groupJoinUserList;
        for (var i = 0; i < groupJoinUserList.length; i++) {
          var groupJoinUser = groupJoinUserList[i];
          if (groupJoinUser.ischeck) {
            var json = { "joinid": groupJoinUser.joinid };
            joinUser.push(json);
          }
        }

        var data = {};
        data.openid = openid;
        data.access_token = res.access_token;
        data.circleid = _this.data.circleid;
        data.groupid = _this.data.group.id;
        data.joinUser = JSON.stringify(joinUser);
        util.post(api.Home_SavegroupJoinUserApiUrl, data).then(function (res) {
          if (res.code == 200) {
            wx.hideLoading();
            wx.navigateBack({
              delta: 1
            })
          }
        })
      })
    }
  },
  
  /**
   * 跳转到添加成员分组的页面
   */
  navigateToGroupInfo: function () {
    var _this = this;
    var groupList = _this.data.groupList;
    var group = _this.data.group;
    var circleid = _this.data.circleid;
    wx.navigateTo({
      url: '../../group/groupInfo/groupInfo?circleid=' + circleid + '&groupList=' + JSON.stringify(groupList) + '&group=' + JSON.stringify(group)
    })
  },

  /**
   * 进入用户主页
   */
  navigateToPersonhome: function (e) {
    var openid = e.currentTarget.dataset.openid;
    wx.navigateTo({
      url: '../../../../../personhome/personhome?openid=' + openid
    })
  },
})